//
//  TabBar.m
//  JLZhiBo
//
//  Created by apple on 15/12/29.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "BaseTabBar.h"
#import "BaseButton.h"
#import "TabBarButton.h"
#import "JLXXDefine.h"



#import "UIView+JLXXFrame.h"

@interface BaseTabBar ()

@property (nonatomic , strong) UIButton *centerButton;

@end

@implementation BaseTabBar

-(instancetype)init{
    
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

-(void)centerButtonClick
{
    if ([self.baseDelegate respondsToSelector:@selector(baseTabBar:didClickCenterButton:)])
    {
        [self.baseDelegate baseTabBar:self didClickCenterButton:_centerButton];
    }
}

/**
 *  想要重新排布系统控件subview的布局，推荐重写layoutSubviews，在调用父类布局后重新排布。
 */
- (void)layoutSubviews
{
    [super layoutSubviews];

    int btnIndex = 0;
    for (UIView *btn in self.subviews) {
        //遍历tabbar的子控件
        //btn是_UIBarBackground,则btn.superclass是UIView,排除.
        //btn是UITabBarButton则btn.superclass是UIControl,调整子控件位置，空出中间位置.
        if ([btn.superclass isSubclassOfClass:[UIControl class]]) {
            btn.width = self.width / 5;
            btn.x = btn.width * btnIndex;
            btnIndex ++;
            if (btnIndex == 2) {
                btnIndex ++;
            }
        }
    }
    [self addSubview:self.centerButton];
    [self bringSubviewToFront:self.centerButton];
    self.centerButton.yzgCenterX = self.center.x;
    self.centerButton.yzgCenterY = self.height/2.0 - 10;
}
-(UIButton *)centerButton{
    if(!_centerButton){
        UIButton *centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *image = [UIImage imageNamed:@"直播"];
        [centerButton setImage:image forState:UIControlStateNormal];
        [centerButton setImage:image forState:UIControlStateHighlighted];
        [centerButton sizeToFit];
        [centerButton addTarget:self action:@selector(centerButtonClick) forControlEvents:UIControlEventTouchUpInside];
        self.centerButton = centerButton;
    }
    return _centerButton;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    //这一个判断是关键，不判断的话push到其他页面，点击发布按钮的位置也是会有反应的，这样就不好了
    //self.isHidden == NO 说明当前页面是有tabbar的，那么肯定是在导航控制器的根控制器页面
    //在导航控制器根控制器页面，那么我们就需要判断手指点击的位置是否在发布按钮身上
    //是的话让发布按钮自己处理点击事件，不是的话让系统去处理点击事件就可以了
    if (self.isHidden) {
        return [super hitTest:point withEvent:event];
    }else{
        CGRect centerButtonFrame = self.centerButton.frame;
        BOOL isCenterButtonFrame = CGRectContainsPoint(centerButtonFrame, point);
        if (isCenterButtonFrame){
            return self.centerButton;
        }else{
            return [super hitTest:point withEvent:event];
        }
    }
}

-(void)dealloc{
    YZGLog(@"%@",[self class]);
}

@end
